package com.View;

import com.model.Anggota;
import com.model.Perpustakaan;
import com.model.Publikasi;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class PanelTambahAnggota extends JPanel {

    private JLabel form;
    private JLabel lidAnggota;
    private JLabel lnama;
    private JTextField tidAnggota;
    private JTextField tnama;
    private JButton tombolSimpan;
    private JButton tombolCancel;

    public PanelTambahAnggota() {
        this.setVisible(true);
        this.setLayout(null);
        form = new JLabel("FORM TAMBAH ANGGOTA ");
        form.setBounds(100, 1, 200, 20);
        add(form);
        lidAnggota = new JLabel("ID Anggota");
        lidAnggota.setBounds(20, 50, 100, 20);
        add(lidAnggota);
        lnama = new JLabel("Nama");
        lnama.setBounds(20, 75, 100, 20);
        add(lnama);
        tidAnggota = new JTextField();
        tidAnggota.setBounds(125, 50, 300, 20);
        add(tidAnggota);
        tnama = new JTextField();
        tnama.setBounds(125, 75, 300, 20);
        add(tnama);
        tombolSimpan = new JButton("Simpan");
        tombolSimpan.setBounds(100, 150, 100, 30);
        add(tombolSimpan);
        tombolSimpan.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                tombolSimpanAction();
            }
        });
        tombolCancel = new JButton("Cancel");
        tombolCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                exitCommand();
            }
        });
        tombolCancel.setBounds(250, 150, 100, 30);
        add(tombolCancel);
    }

    private void tombolSimpanAction() {
        FileInputStream inStream = null;
        File inFile = new File("anggota.dat");
        try {
            inStream = new FileInputStream(inFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
        ObjectInputStream inObjectStream = null;
        try {
            inObjectStream = new ObjectInputStream(inStream);
        } catch (IOException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Perpustakaan.DAFTAR_ANGGOTA = (ArrayList<Anggota>) inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void exitCommand() {
        System.exit(0);
    }
}
